﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using HZ.Supervise.Model;
namespace HZ.Supervise.Web.Secondphase.ReportCompany
{
    public partial class CompanyCurrentGoodsList : BasePage
    {
        private string strcompanyps = "";//医院名称
        private string strMosth = "";//月份
        string append = "";
        string sortField = "PurchaseTimeMonth ASC";
        private bool hide = true;
        GroupByField groupField = GroupByField.PurchaseTimeMonth | GroupByField.SortSource | GroupByField.SortID;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ViewState["query"] = URLUtil.GetSuperviseQueryFromURL(Request);
                SuperviseQuery sq = (SuperviseQuery)ViewState["query"];

                if (sq.SortSource == 0)
                {
                    droSort.SelectedIndex = 1;
                }
                else if (sq.SortSource == 1)
                {
                    droSort.SelectedIndex = 2;
                }
                else if (sq.SortSource == 2)
                {
                    droSort.SelectedIndex = 3;
                }
                BindList();
                GetStatistics();
            }
        }
        void BindList()
        {
            SuperviseQuery sq = (SuperviseQuery)ViewState["query"];

            var model = new HZ.Data.QueryModel();
            model["AreaId"] = sq.AreaID;
			model["CompanyName"] = sq.CompanyName_PS;
            model["DayTime"] = sq.Mid;

            if (!this.droSort.SelectedValue.Equals("-1"))
            {
                model["SortSorce"] = droSort.SelectedValue;
            }

            model["SortFields"] = this.droSortField.SelectedValue;

            int t = 0;

            HZ.Supervise.IBLL.IBLL_NewStat iNew = HZ.Supervise.BLLFactory.IBLL_NewStats();
            DataTable dt = new DataTable();
            if (sq.CompanyType == 0)//生产
            {
                dt = iNew.DayTime_SortSorec_CompanyNameSC_GroupBy(model, AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out t);
            }
            else
            {
                dt = iNew.DayTime_SortSorec_CompanyName_GroupBy(model, AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out t);
            }
           
            AspNetPager1.RecordCount = t;
            gvwSortInfo.DataSource = dt;
            HZ.Supervise.Web.Function.AddColumn(dt, "SortSorce");
            HZ.Supervise.Web.Function.ChangeRate(dt, "WarehouseRate");
            HZ.Supervise.Web.Function.ChangeRate_PS(dt, "DistributeRate");
            gvwSortInfo.DataBind();

            HZ.Supervise.Web.Function.Combine(gvwSortInfo, 0, 0);
            HZ.Supervise.Web.Function.Combine(gvwSortInfo, 0, 7);

        }
        /// <summary>
        /// 绑定统计    by张雄  2011.5.10
        /// </summary>
        void GetStatistics()
        {
            double purchaseAmountTotal = 0;//采购总金额
            double wareHouseAmountTotal = 0;//入库总金额
            double distributeAmountTotal = 0;//配送总金额
            for (int i = 0; i < gvwSortInfo.Rows.Count; i++)
            {
                purchaseAmountTotal += double.Parse(gvwSortInfo.Rows[i].Cells[2].Text);
                wareHouseAmountTotal += double.Parse(gvwSortInfo.Rows[i].Cells[5].Text);
                distributeAmountTotal+= double.Parse(gvwSortInfo.Rows[i].Cells[3].Text);
            }
            this.lblCount.Text = "采购总金额为:" + purchaseAmountTotal.ToString() + ";配送总金额为:" + distributeAmountTotal.ToString() + ";入库总金额为:" + wareHouseAmountTotal.ToString();

        }

        /// <summary>
        /// 合并表头
        /// 张雄 2011.4.26 create
        /// </summary>
        /// <param name="sender">事件发送源</param>
        /// <param name="e">事件参数</param>
        protected void gvwSortInfo_RowCreated(object sender, GridViewRowEventArgs e)
        {
            #region 合并表头
            switch (e.Row.RowType)
            {
                case DataControlRowType.Header:

                    //取得下拉框的月份
                    //第一行表头
                    TableCellCollection tcHeader = e.Row.Cells;
                    tcHeader.Clear();

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[0].Attributes.Add("colspan", "8"); //跨Column
                    tcHeader[0].Attributes.Add("bgcolor", "LightSteelBlue");
                    SuperviseQuery sq = (SuperviseQuery)ViewState["query"];
                    tcHeader[0].Text = ForMateDate(sq.Mid) + " " + sq.CompanyName_PS + "</th></tr><tr>";

                    //第2行表头
                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[1].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[1].Text = "企业名称";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[2].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[2].Text = "采购类型";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[3].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[3].Text = "采购总金额";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[4].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[4].Text = "配送总金额";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[5].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[5].Text = "配送率";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[6].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[6].Text = "入库总金额";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[7].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[7].Text = "入库率";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[8].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[8].Text = "操作</th></tr><tr>";
                    break;
            }
            #endregion
        }

        /// <summary>
        /// 格式化日期
        /// 张雄 2011.4.26 create
        /// </summary>
        /// <returns></returns>
        private string ForMateDate(string Str)
        {
            string YearStr = String.Empty;

            string MonStr = String.Empty;

            if (!String.IsNullOrEmpty(Str))
            {
                if (Str.Length == 6)
                {
                    //取得年
                    YearStr = Str.Substring(0, 4) + "年";

                    //取得月
                    MonStr = Str.Substring(4, 2);
                    if (MonStr.Substring(0, 1) == "0")
                    {
                        MonStr = MonStr.Substring(1, 1) + "月";
                    }
                    else
                    {
                        MonStr += "月";
                    }
                }
            }

            return Str = YearStr + MonStr;
        }


        protected void gvwSortInfo_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "DirectByDetail")
            {
           
                string sessionid = Guid.NewGuid().ToString();
                SuperviseQuery query = (SuperviseQuery)ViewState["query"];
                query.SortSource = Convert.ToInt32(this.droSort.SelectedValue);
                var model = new HZ.Data.QueryModel();
                model["AreaId"] = query.AreaID;    //把AreaId添加到Session的集合中
                Session[sessionid] = query;
                string url = "CompanyGoodsList.aspx";
                Response.Redirect(string.Format("{0}?sessionid={1}", url, sessionid));
            }
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            if (!this.droSort.SelectedValue.Equals("-1"))
            {
                append = "AND SortSource = " + droSort.SelectedValue;
            }
            else
            {
                groupField = GroupByField.PurchaseTimeMonth | GroupByField.SortID;
                hide = false;
            }
            sortField = this.droSortField.SelectedValue;
            //this.hidSortSource.Value = this.droSort.SelectedValue.ToString();
            BindList();
			GetStatistics();
        }

     

        protected void gvwSortInfo_RowDataBound(object sender, GridViewRowEventArgs e)
        {
 
        }

        /// <summary>
        /// 页面分页
        /// by张雄  2011.5.8
        /// </summary>
        /// <param name="sender">事件发送源</param>
        /// <param name="e">参数</param>
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindList();
        }

        /// <summary>
        /// 导出Excel
        /// by张雄  2011.5.16
        /// </summary>
        /// <param name="sender">事件的发送源</param>
        /// <param name="e">事件参数</param>
        protected void btnExcel_Click(object sender, EventArgs e)
        {
            SuperviseQuery sq = (SuperviseQuery)ViewState["query"];

            var model = new HZ.Data.QueryModel();
            model["AreaId"] = sq.AreaID;
            model["CompanyName"] = sq.CompanyName_PS;
            model["DayTime"] = sq.Mid;
            
            if (!this.droSort.SelectedValue.Equals("-1"))
            {
                model["SortSorce"] = droSort.SelectedValue;
            }

            model["SortFields"] = this.droSortField.SelectedValue;

            int t = 0;

            HZ.Supervise.IBLL.IBLL_NewStat iNew = HZ.Supervise.BLLFactory.IBLL_NewStats();

            DataTable dt = new DataTable();
            if (sq.CompanyType == 0)//生产
            {
                dt = iNew.DayTime_SortSorec_CompanyNameSC_GroupBy(model, 1, 0, out t);
            }
            else
            {
                dt = iNew.DayTime_SortSorec_CompanyName_GroupBy(model,1, 0, out t);
            }
            gvwSortInfo.DataSource = dt;
            HZ.Supervise.Web.Function.AddColumn(dt, "SortSorce");
            HZ.Supervise.Web.Function.ChangeRate(dt, "WarehouseRate");
            HZ.Supervise.Web.Function.ChangeRate_PS(dt, "DistributeRate");
            gvwSortInfo.DataBind();

            HZ.Excel.ExcelHelper.ExportDataForWeb(dt,this.gvwSortInfo);
        }
    }
}
